package edu.fudan.cse.medlab.event.filter;

import java.io.File;

public class FilterByPrefix {
	CopyFile copyfile;
	PrefixFilter prefixfilter;
	int count;
		
	public FilterByPrefix(String prefixfile,String locationfile){
		copyfile = new CopyFile();
		prefixfilter = new PrefixFilter(prefixfile,locationfile,new Settings());
		count = 0;
	}

	public void filterDir(String filestring,String path) throws Exception{
		File f = new File(filestring);
		if(!f.isDirectory()){
			this.filterFile(filestring,path);
			return;
		}
		//System.out.println(f.getAbsolutePath());
		String[] s = f.list();
		if (s==null)
			return;
		for (int i = 0; i < s.length; i++) {
			filterDir(filestring+"\\"+s[i],path);
		}
	}
		
		public void filterFile(String filestring,String path)throws Exception{
			if(prefixfilter.filterURLByPrefix(filestring)){			//ǰ׺
				this.copyfile.copy(filestring,path);
				System.out.println("select page:"+filestring);
				edu.fudan.cse.medlab.event.extraction.DB.addLog("filter select page:"+filestring);
				count++;
			}
			else{
				System.out.println("ignore page:"+filestring);
				edu.fudan.cse.medlab.event.extraction.DB.addLog("filter ignore page:"+filestring);
			}
		}
		
		public static void main(String[] args){
		//	FilterByPrefix fp = new FilterByPrefix("E:\\jmj\\prefix(08-12).txt");
			FilterByPrefix fp = new FilterByPrefix("urlpattern-eventnum-all.txt","location.txt");
			try{
				String oldpath = "E:\\jmj\\sitemap\\target";
				//String oldpath= new String("E:\\jmj\\s");
				//String newpath = "filter page(new pattern+time+loc)";
				String newpath = "filter target";
		//		String newpath = "filter page(prefix)(time+loc)";
				fp.filterDir(oldpath,newpath);
				System.out.println(fp.count);
			}
			catch(Exception e){
				e.printStackTrace();
			}
		}
	}